package com.fyy.netty.chapter01.c1;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author fyy
 * @Description Path测试
 * @Date 2021/4/21 10:26
 */
public class PathFileTest {

    public static void main(String[] args) throws IOException {
        //文件是否存在
        Path path = Paths.get("F:\\test.txt");
        System.out.println(Files.exists(path));
        //创建一级目录
        Path path1 = Paths.get("F:\\test2");
        System.out.println(Files.createDirectory(path1));
        //创建多级目录
        Path path2 = Paths.get("F:\\test2\\eee\\222");
        System.out.println(Files.createDirectories(path2));
        //文件拷贝
        Path source = Paths.get("F:\\test.txt");
        Path target = Paths.get("F:\\test2.txt");
        Files.copy(source,target);
        //覆盖拷贝REPLACE_EXISTING
        Files.copy(source,target, StandardCopyOption.REPLACE_EXISTING);
        //文件移动ATOMIC_MOVE保证文件移动的原子性
        Files.move(source,target,StandardCopyOption.ATOMIC_MOVE);
        //文件删除
        Files.delete(source); //文件不存在：NoSuchFileException
        Files.deleteIfExists(source);
        //删除目录:存在内容：DirectoryNotEmptyException
        Files.delete(target);
        Files.walkFileTree(Paths.get("F:\\test2"),new SimpleFileVisitor<Path>(){
            //进入文件前
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.deleteIfExists(file);
                return super.visitFile(file, attrs);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.deleteIfExists(dir);
                return super.postVisitDirectory(dir, exc);
            }
        });
        //遍历文件
        AtomicInteger dirCount = new AtomicInteger();
        AtomicInteger fileCount = new AtomicInteger();
        Files.walkFileTree(Paths.get("F:\\"),new SimpleFileVisitor<Path>(){
            //进入目录前
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                dirCount.incrementAndGet();
                return super.preVisitDirectory(dir, attrs);
            }

            //进入文件前
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                fileCount.incrementAndGet();
                return super.visitFile(file, attrs);
            }
        });
        //多级目录拷贝
        String sourcePath = "F:\\test";
        String targetPath = "F:\\test2";
        Files.walk(Paths.get(sourcePath)).forEach(path3 -> {
            try {
                String targetName = path3.toString().replace(sourcePath,targetPath);
                if(Files.isDirectory(path3)){
                    //创建拷贝目录
                    Files.createDirectory(Paths.get(targetName));
                }
                if(Files.isRegularFile(path3)){
                    //拷贝文件
                    Files.copy(path3,Paths.get(targetName));
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        });


    }

}
